﻿#include "mydn.h"
#include "mydnapp.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#if defined(XP_UNIX)
#else
#include <io.h>
#endif

#ifndef HIBYTE
#define HIBYTE(x) ((((uint32)(x)) & 0xff00) >> 8)
#endif

NPNetscapeFuncs*	gpnpf		= 0;

/*
 * 描述信息
 *
 */
char* NP_GetMIMEDescription(void)
{
	return (char *)"application/x-mydn-localstorage:mydn:Plug-in for mydn";
}

#if defined(XP_UNIX)
/*
 * Linux/Unix版本
 */
NPError OSCALL NP_Initialize(NPNetscapeFuncs* npnf, NPPluginFuncs* nppfuncs)
{
	NPError 		err			= NPERR_NO_ERROR;
	PRBool			supXEmbed	= PR_FALSE;
	NPNToolkitType	toolkit		= (NPNToolkitType)0;

	// 确定浏览器支持
	
	*nppfuncs = CMydnApp::PluginFuncs;
	err = npnf->getvalue(NULL, NPNVSupportsXEmbedBool, (void *)&supXEmbed);
	if(err != NPERR_NO_ERROR || supXEmbed != PR_TRUE)
		return NPERR_INCOMPATIBLE_VERSION_ERROR;

	err = npnf->getvalue(NULL, NPNVToolkit, (void *)&toolkit);
	if(err != NPERR_NO_ERROR || NPNVGtk2 != toolkit)
		return NPERR_INCOMPATIBLE_VERSION_ERROR;

	
	if(npnf == NULL)
		return NPERR_INVALID_FUNCTABLE_ERROR;

	if(HIBYTE(npnf->version) > NP_VERSION_MAJOR)
		return NPERR_INCOMPATIBLE_VERSION_ERROR;

	gpnpf = npnf;

	return NPERR_NO_ERROR;
}
NPError NP_GetValue(void *future, NPPVariable aVariable, void *aValue)
{
	CMydnApp::_class.GetValue((NPP)future, aVariable, aValue);
}

char* NP_GetPluginVersion()
{
	return (char *)"v1.0";
}

#else
// windows 版本
NPError OSCALL NP_GetEntryPoints(NPPluginFuncs *nppfuncs) 
{
	*nppfuncs = CMydnApp::PluginFuncs;

	return NPERR_NO_ERROR;
}

NPError OSCALL NP_Initialize(NPNetscapeFuncs *npnf)
{
	if(npnf == NULL)
		return NPERR_INVALID_FUNCTABLE_ERROR;

	if(HIBYTE(npnf->version) > NP_VERSION_MAJOR)
		return NPERR_INCOMPATIBLE_VERSION_ERROR;

	gpnpf = npnf;

	return NPERR_NO_ERROR;
}

#endif // XP_UNIX

NPError OSCALL NP_Shutdown() 
{
	gpnpf = NULL;

	return NPERR_NO_ERROR;
}

////////////////////////////////////////////////////////////////////////////////////////
// 文件合并
char* mydn::pathCombine(char *pNewPath, size_t size, const char *pDir, const char *file, const char split)
{
    size_t      nLen        = strlen(pDir);
    size_t      nFileLen    = strlen(file);

    if(size < (nLen + nFileLen + 1))
        return pNewPath;
    strcpy(pNewPath, pDir);
    if(0 == nLen)
    {
        pNewPath[0] = split;
        pNewPath[1] = 0;
    }
    else if('/' != pNewPath[nLen - 1] && '\\' != pNewPath[nLen - 1])
    {
        pNewPath[nLen] = split;
        pNewPath[nLen + 1] = 0;
    }
    strcat(pNewPath, file);
    return pNewPath;
}

// 检查文件是否HTML后缀
bool mydn::checkPathExtensionIsHtml(const char *pPath)
{
    const char*     pExt            = strrchr(pPath, '.');
    char            szExt[32]       = {0};

    if(NULL == pExt)
        return false;
    strncpy(szExt, pExt, sizeof(szExt));
    if(strcmp(szExt, ".html") == 0
            || strcmp(szExt, ".htm") == 0)
    {
        return true;
    }

    return false;
}

// 得到HTML数据
bool mydn::getHtmlData(std::string &strHtml, const char *pHtmlFile)
{
	int					fd			= open(pHtmlFile, O_RDONLY);
    struct stat         st          = {0};
	int					rd			= 0;

    if(-1 == fd)
    {
        DbgTrace(("Can't open file: %s\n", pHtmlFile));
        return false;
    }
	fstat(fd, &st);
    if(0 >= st.st_size)
        return false;

    strHtml.resize(st.st_size + 1, 0);
    if((rd = read(fd, (void *)strHtml.c_str(), st.st_size)) <= 0)
	{
		perror("read");
	}

    close(fd);
    return true;
}



